function Game(cfg) {
  for (var attr in cfg) {
    this[attr] = cfg[attr];
  }
}

Game.prototype = {
  constructor: Game ,
  
  canvas: null,
  context: null,

  FPS: 40,
  sleep: 0,

  sprites: null,
  
  init: function() {
    if (this.FPS) {
      this.sleep = Math.floor(1000 / this.FPS);
    }
    
    this.sprites = this.sprites || [];
    this.sprites.forEach(function(sprite) {
      sprite.init();
    });
  },

  addSprite: function(sprite) {
    this.sprites.push(sprite);
    sprite.init();
  },

  removeSprite: function(name) {
    var sprites = this.sprites;
    for (var i = 0, length = sprites.length; i < length; i++) {
      if (name == sprites[i].name) {
        sprites.splice(i, 1);
        return;
      }
    }
  },
  
  start: function() {
    var that = this;
    
    // Main loop.
    this.mainLoop = setInterval(function() {
      var deltaTime = that.sleep;
      
      that.update(deltaTime);
      that.clear(deltaTime);
      that.draw(deltaTime);
      
    }, that.sleep);
  
  },

  update: function(deltaTime) {
    this.sprites.forEach(function(sprite) {
      sprite.update(deltaTime);
    });
  },

  clear: function() {
    var canvas = this.canvas;
    var mapImage = Resource.get('mapImage');
    this.context.drawImage(mapImage, 0, 0, canvas.width, canvas.height);
  },
  
  draw: function() {
    var that = this;
    this.sprites.forEach(function(sprite) {
      sprite.draw(that.context);
    });
  }
};